

global filename jmp_simulation_outcomes_combined



use "stata data/outcomes day_scenario all_days new price change 6 sensitivity scalar 1.dta", clear

foreach day_scenario in all_days top_3 trigger_day_keeper  {
append using "stata data/outcomes day_scenario `day_scenario' new price change 6 sensitivity scalar 1.dta"
}

foreach day_scenario in all_days top_3 trigger_day_keeper  {
append using "stata data/outcomes day_scenario `day_scenario' new price change 11 sensitivity scalar 1.dta"
}


foreach day_scenario in all_days top_3 trigger_day_keeper  {
append using "stata data/outcomes day_scenario `day_scenario' new price change 15 sensitivity scalar 1.dta"
}


drop discounted_value_highcs discounted_value_sim_highcs



duplicates drop

keep discounted_value* day_scenario simulated_price

sort day_scenario simulated*

capture drop group
egen group = group(simulated)

drop simulated

drop discounted_value

keep discounted_value_sim_ces day_scenario group discounted_value_sim

reshape wide discounted_value*, i(day_scenario) j(group)

preserve
keep day_scenario discounted_value_sim_ces1	discounted_value_sim_ces2	discounted_value_sim_ces3
rename discounted_value_sim_ces1 discounted_value_sim1		
rename discounted_value_sim_ces2 discounted_value_sim2		
rename discounted_value_sim_ces3 discounted_value_sim3
gen ces = 1
save "stata data/temp append", replace
restore 		

drop discounted_value_sim_ces1	discounted_value_sim_ces2	discounted_value_sim_ces3
gen ces = 0
append using "stata data/temp append"




//drop the traditional ones used in the other figure
capture drop discounted_value_sim1	discounted_value_sim_ces_highcs1	discounted_value_sim2	discounted_value_sim_ces_highcs2	discounted_value_sim3	discounted_value_sim_ces_highcs3


rename discounted_value_sim1 discounted_value_sim_60
rename discounted_value_sim2 discounted_value_sim_110
rename discounted_value_sim3 discounted_value_sim_150


order day discounted_value_sim_60 discounted_value_sim_110 discounted_value_sim_150

foreach var in discounted_value_sim_60 discounted_value_sim_110 discounted_value_sim_150 {
replace  `var' = round(`var'/1000000)
}

gen sort_order = 1
replace sort_order = 2 if day_scenario == "trigger_day_keeper"
replace sort_order = 3 if day_scenario == "top_3"

sort ces sort_order

save "stata data/simulated values sim_ces_combined", replace

use "stata data/simulated values sim_ces_combined", clear
*format v* %15.0fc
*xpose, clear varname


capture file close table1		
file open table1 using "latex/$filename.tex", write text replace


file write table1 "\label{$filename}" _n
file write table1 "\label{ttest table}" _n
file write table1 "\vspace{-2mm}" _n
file write table1 "\footnotesize" _n
file write table1 "\begin{tabular}{@{\extracolsep{4pt}}lccc@{}ccccc}" _n //this is where you change justification. Options are l c r
*extracolsep from http://tex.stackexchange.com/questions/101000/how-to-shorten-cline-so-that-multiple-groups-in-a-table-become-visible

file write table1 "\hline\hline \\ [-.9em]" _n 

*file write table1 "\multicolumn{1}{c}{$\begin{matrix}\text{Baseline}\\ \text{Territory} \end{matrix}$}& 	\multicolumn{1}{c}{$\begin{matrix}\text{Average}\\ \text{Temperature} \end{matrix}$} &	\multicolumn{1}{c}{$\begin{matrix}\text{Firm}\\ \text{Count} \end{matrix}$}	&	\\" _n
*file write table1 " Energy use classification && Median & & &Thirds	\\" _n 
*file write table1 "  \cline{2-4} \cline{5-7} \\" _n 
*file write table1 "[0.2em]" _n

file write table1 "  & (1) & (2) & (3)  \\" _n
file write table1 "[0.2em]" _n


local col1_top "Scenario"
local col1_bottom ""
local col2_top "\char36 .85/kWh peak"
local col2_bottom "(current price)"
local col3_top "\char36 1.35/kWh peak"
local col3_bottom "(large C\&I  peak price)"
local col4_top "\char36 1.85/kWh peak"
local col4_bottom "(high price)"



local col1 "\multicolumn{1}{c}{$\begin{matrix}\text{`col1_top'}\\ \text{`col1_bottom'} \end{matrix}$}"
local col2 "\multicolumn{1}{c}{$\begin{matrix}\text{`col2_top'}\\ \text{`col2_bottom'} \end{matrix}$}"
local col3 "\multicolumn{1}{c}{$\begin{matrix}\text{`col3_top'}\\ \text{`col3_bottom'} \end{matrix}$}"
local col4 "\multicolumn{1}{c}{$\begin{matrix}\text{`col4_top'}\\ \text{`col4_bottom'} \end{matrix}$}"
local col5 "\multicolumn{1}{c}{$\begin{matrix}\text{`col5_top'}\\ \text{`col5_bottom'} \end{matrix}$}"



file write table1 " `col1_top' & `col2' & `col3' & `col4'  	\\" _n



*file write table1 "  Event Date & All PG\&E average temperature& Coastal firm average temperatures & Inland firm average temperatures \\" _n
file write table1 "[0.1em]" _n
file write table1 "\hline \\" _n
file write table1 "[-0.4em]" _n

file write table1 "\underline{Panel A: Linear demand}\\" _n

file write table1 "[0.2em]" _n

capture gen row = _n

replace day_scenario = "15 days/summer" if day_scenario == "all_days"
replace day_scenario = "101 degree trigger (8 days)" if day_scenario == "trigger_day_keeper"
replace day_scenario = "Super-peak days (3 days)" if day_scenario == "top_3"





gen id = _n


tostring discounted_value_sim_*, replace

foreach var of varlist discounted_value_sim_*{
replace `var' = "\\$"+ `var'
}


levelsof id if ces ==0, local(vars)
foreach v of local vars  {
  qui sum row if id==`v'
  local r = r(mean)
  
  	
	local r = r(mean)
	local t1 = day_scenario[`r']
	local t2 = discounted_value_sim_60[`r']
	local t3 =  discounted_value_sim_110[`r']
	local t4 = discounted_value_sim_150[`r']
	*local t5 = discounted_value_sim_180[`r']
	
	

	file write table1 "\hspace{.1in} `t1' & `t2' & `t3' & `t4'    \\" _n
	file write table1 "[0.2em] " _n
	}

*
file write table1 " \\" _n
*file write table1 "[0.1em]" _n
*file write table1 "\hline \\" _n


*file write table1 "[-0.4em]" _n
file write table1 "\underline{Panel B: Constant elasticity demand}\\" _n
file write table1 "[0.2em]" _n


levelsof id if ces ==1, local(vars)
foreach v of local vars  {
  qui sum row if id==`v'
  local r = r(mean)
  
  	
	local r = r(mean)
	local t1 = day_scenario[`r']
	local t2 = discounted_value_sim_60[`r']
	local t3 =  discounted_value_sim_110[`r']
	local t4 = discounted_value_sim_150[`r']
	*local t5 = discounted_value_sim_180[`r']
	
	

	file write table1 "\hspace{.1in} `t1' & `t2' & `t3' & `t4'    \\" _n
	file write table1 "[0.2em] " _n
	}

*

	




file write table1 "\hline" _n
file write table1 "\end{tabular}" _n

file close table1		





